﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using Telerik.Web.UI;
using System.Data;

namespace RN.ERP.Web.Person
{
    public partial class PersonEmpStatistic3 : BasePage
    {
        protected string AreaCode
        {
            get
            {
                return UserInfo.CurrentArea;
            }
        }
        /// <summary>
        /// 作为查询地区限定条件
        /// </summary>
        protected string AreaCodeWhere
        {
            get
            {
                return UserInfo.LocalArea;
            }
        }
        /// <summary>
        /// 保存人员类型
        /// </summary>
        private List<ModelService.T_Sys_Dict> dict
        {
            get
            {
                if (ViewState["dict"] != null)
                {
                    return JsonConvert.DeserializeObject<List<ModelService.T_Sys_Dict>>(ViewState["dict"].ToString());
                    //return (List<ModelService.T_Sys_Dict>)ViewState["dict"];
                }
                return JsonConvert.DeserializeObject<List<ModelService.T_Sys_Dict>>(GetGetDictionary());
            }

        }
        public override bool IsCheckPermission
        {
            get { return false; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //绑定列表
                rgPersonList.DataSource = ToDataTable();
              
            }
        }
       

        /// <summary>
        /// 取得人员类型数据源
        /// </summary>
        /// <returns></returns>
        protected string GetGetDictionary()
        {
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
               // var dictTmp = JsonConvert.DeserializeObject<List<ModelService.T_Sys_Dict>>(client.GetExpertSpecList(AreaCode));
                string dictTmp = client.GetExpertSpecList(AreaCode);
                ViewState["dict"] = dictTmp;
                return dictTmp;
            }
        }

        /// <summary>
        /// 格式化结果集
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rgPersonList_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridHeaderItem)
            {
                ///隐藏地区号
                GridHeaderItem gridHearItem = e.Item as GridHeaderItem;
                gridHearItem["DBType"].Visible = false;
                gridHearItem["分库"].CssClass = "fixColleft";
            }
            int gridCount = 0;
            if (e.Item is GridDataItem)
            {
                GridDataItem gridItem = e.Item as GridDataItem;
                gridItem["DBType"].Visible = false;
                gridItem["分库"].CssClass = "fixColleft";
                gridItem["分库"].Text = "<a href='PersonExpertList.aspx?flowcode=1,019,029,039,049&areacode=" + gridItem["DBType"].Text + "'><span style='color: #43a9f5'>" + gridItem["分库"].Text + "</span></a>";
                ///根据人员类型列表取得相应列并将统计数目大于零的加链接
                 List<ModelService.T_Sys_Dict> tmps = dict.Where(t => t.ParentSID>0).ToList();
                foreach (var d in tmps)
                {
                    if (gridItem[d.Code] != null)
                    {
                        int.TryParse(gridItem[d.Code].Text, out gridCount);
                        if (gridCount > 0)
                        {
                            gridItem[d.Code].Text = "<a href='PersonExpertList.aspx?flowcode=1,019,029,039,049&areacode=" + gridItem["DBType"].Text + "&sid=" + d.Code + "'><span style='color: #43a9f5'>" + gridCount.ToString() + "</span></a>";
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 审核状态查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rblFlowCodeList_SelectedIndexChanged(object sender, EventArgs e)
        {
            rgPersonList.DataSource = ToDataTable();
            rgPersonList.DataBind();
        }

        /// <summary>
        /// 取得数据源并将数据源转化成DataTable
        /// </summary>
        /// <returns></returns>
        public DataTable ToDataTable()
        {
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                //取得统计数据结果集
                var statist = JsonConvert.DeserializeObject<ModelService.StatisticsStruct>(client.GetPersonExpertStatistic3(AreaCodeWhere.Trim(), AreaCode));
                //转化成DataTable
                DataTable result = UtilitiesTool.StructToDataTable(statist);             
                return result;
            }
        }
        /// <summary>
        /// 创建表头
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rgPersonList_ItemCreated(object sender, GridItemEventArgs e)
        {
            TableHeaderCell tableCell;
            switch (e.Item.ItemType)
            {
                case Telerik.Web.UI.GridItemType.Header:
                    //第一行表头
                    TableCellCollection tcHeader = e.Item.Cells;

                    tcHeader.Clear();
                    tableCell = new TableHeaderCell();
                    tableCell.RowSpan = 2;
                    tableCell.Text = string.Empty;
                    tcHeader.Add(tableCell);

                    tableCell = new TableHeaderCell();
                    tableCell.ColumnSpan = 1;
                    tableCell.Text = string.Empty;
                    tcHeader.Add(tableCell);
                  
                    tableCell = new TableHeaderCell();
                    tableCell.RowSpan = 2;
                    tableCell.Text = string.Empty;
                    tcHeader.Add(tableCell);

                    tableCell = new TableHeaderCell();
                    tableCell.RowSpan = 2;
                    tableCell.Width = Unit.Point(100);
                    tableCell.Wrap = false;
                    tableCell.Text = "分库";
                    tcHeader.Add(tableCell);

                    List<ModelService.T_Sys_Dict> tmps = dict.Where(t => t.ParentSID == 0).ToList();
                    for (int i = 0; i < tmps.Count; i++)
                    {
                        ModelService.T_Sys_Dict tmp = tmps[i];
                        int count = dict.Where(t => t.ParentSID == tmp.SID).Count();
                        if (count == 0)
                        {
                            continue;
                        }
                        tableCell = new TableHeaderCell();
                        tableCell.ColumnSpan = count;
                        //  tableCell.Attributes.Add("colspan", count.ToString());
                        tableCell.Text = "<Center>" + tmp.Name + "</Center>";
                        if (i == tmps.Count - 1)
                        {
                            tableCell.Text = "<Center>" + tmp.Name + "</Center></th></tr><tr>";
                        }
                        tcHeader.Add(tableCell);
                    }
                   


                    //第二行表头
                    for (int i = 0; i < tmps.Count; i++)
                    {
                        ModelService.T_Sys_Dict tmp = tmps[i];
                        List<ModelService.T_Sys_Dict> tmps1 = dict.Where(t => t.ParentSID == tmp.SID).ToList();
                        for (int j = 0; j < tmps1.Count; j++)
                        {
                            ModelService.T_Sys_Dict tmp1 = tmps1[j];
                            tableCell = new TableHeaderCell();
                            tableCell.Text = "<Center>" + tmp1.Name + "</Center>";
                            //if (j == 0)
                            //{
                            //    tableCell.Text = "<Center>" + tmp1.Name + "</Center></tr><tr>";
                            //}
                            tcHeader.Add(tableCell);
                        }
                    }
                    break;
            }
        }
    }
}